﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;
    using WMS6.Web.UserControls;

    public class CCReleaseLogList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5522;
        protected GridView gvCCRelease;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblCCRequestList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataPhysicalRequestList(CycleCountReleaseQueryEntity CycleCountReleaseQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(CycleCountReleaseQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = StockCountServiceClientProxy.GetInstance().GetCycleCountReleaseByQueryEntity(CycleCountReleaseQueryEntity);
            base.BindData2GridView(this.gvCCRelease, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvCCRelease, "RELEASE_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("CCReleaseLog.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity RequestQueryEntity = this.GetCycleCountReleaseQueryEntity();
            RequestQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = RequestQueryEntity.CurrentPage;
            RequestQueryEntity.CurrentPage = "0";
            this.gvCCRelease.Columns[1].Visible = false;
            this.gvCCRelease.AllowSorting = false;
            this.BindDataPhysicalRequestList(RequestQueryEntity);
            this.hdnActionID.Value = "";
            this.gvCCRelease.ShowFooter = false;
            RequestQueryEntity.IsGetAll = false;
            RequestQueryEntity.CurrentPage = currentPageIndex;
            this.SetCycleCountReleaseQueryEntity(RequestQueryEntity);
            base.Export2Excel("CCReleaseLog.xls", this.gvCCRelease);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
            CycleCountReleaseQuery.CurrentPage = "0";
            this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
            CycleCountReleaseQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
            CycleCountReleaseQuery.ReleaseID = "";
            CycleCountReleaseQuery.OwnerID = "";
            CycleCountReleaseQuery.SkuID = "";
            CycleCountReleaseQuery.Loc = "";
            CycleCountReleaseQuery.CreatedBy = "";
            CycleCountReleaseQuery.ReleaseDate = "";
            this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
            if (int.Parse(CycleCountReleaseQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                CycleCountReleaseQuery.CurrentPage = (int.Parse(CycleCountReleaseQuery.CurrentPage) + 1).ToString();
                this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
            if (int.Parse(CycleCountReleaseQuery.CurrentPage) > 0)
            {
                CycleCountReleaseQuery.CurrentPage = (int.Parse(CycleCountReleaseQuery.CurrentPage) - 1).ToString();
                this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
            }
        }

        private CycleCountReleaseQueryEntity GetCycleCountReleaseQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as CycleCountReleaseQueryEntity);
            }
            return (base.PageQueryEntitySession as CycleCountReleaseQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
                CycleCountReleaseQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
            }
        }

        protected void gvCCRelease_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5522", this.gvCCRelease);
        }

        protected void gvCCRelease_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvCCRelease_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                CycleCountReleaseQueryEntity CycleCountReleaseQuery = this.GetCycleCountReleaseQueryEntity();
                CycleCountReleaseQuery.SortField = e.SortExpression;
                if (CycleCountReleaseQuery.SortDirection == "ASC")
                {
                    CycleCountReleaseQuery.SortDirection = "DESC";
                }
                else if (CycleCountReleaseQuery.SortDirection == "DESC")
                {
                    CycleCountReleaseQuery.SortDirection = "ASC";
                }
                this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            CycleCountReleaseQueryEntity CycleCountReleaseQuery = new CycleCountReleaseQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    CycleCountReleaseQuery.ReleaseID = this.PreviousPage.ReleaseID;
                    CycleCountReleaseQuery.OwnerID = this.PreviousPage.OwnerID;
                    CycleCountReleaseQuery.SkuID = this.PreviousPage.SkuID;
                    CycleCountReleaseQuery.Loc = this.PreviousPage.Loc;
                    CycleCountReleaseQuery.CreatedBy = this.PreviousPage.CreateBy;
                    CycleCountReleaseQuery.ReleaseDate = this.PreviousPage.ReleaseDate;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            CycleCountReleaseQuery.IsGetAll = false;
            CycleCountReleaseQuery.CurrentPage = "0";
            CycleCountReleaseQuery.PageSize = this.gvCCRelease.PageSize.ToString();
            CycleCountReleaseQuery.SortField = "CCR.ROWID";
            CycleCountReleaseQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvCCRelease.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetCycleCountReleaseQueryEntity(CycleCountReleaseQuery);
            this.BindDataPhysicalRequestList(CycleCountReleaseQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetCycleCountReleaseQueryEntity(CycleCountReleaseQueryEntity CycleCountReleaseQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = CycleCountReleaseQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = CycleCountReleaseQueryEntity;
            }
        }

        public CCReleaseLogQuery PreviousPage
        {
            get
            {
                return (CCReleaseLogQuery) base.PreviousPage;
            }
        }
    }
}

